﻿/*----------------------------------------------------------------
    Copyright (C) 2015 Senparc
    
    文件名：MenuController.cs
    文件功能描述：自定义菜单设置工具Controller
    
    
    创建标识：Senparc - 20150312
----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Menu;
using ITXHWEB.Controllers;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.CardMember;
namespace Senparc.Weixin.MP.Sample.Controllers
{
    public class MenuController : ITXController
    {
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            GetMenuResult result = new GetMenuResult();

            //初始化
            for (int i = 0; i < 3; i++)
            {
                var subButton = new SubButton();
                for (int j = 0; j < 5; j++)
                {
                    var singleButton = new SingleClickButton();
                    subButton.sub_button.Add(singleButton);
                }
            }

            return View(result);
        }

        [HttpPost]
        public ActionResult CreateMenu(string token, GetMenuResultFull resultFull)
        {
            try
            {
                //重新整理按钮信息
                var bg = CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull).menu;
                var result = CommonAPIs.CommonApi.CreateMenu(ACCESSTOKEN, bg);
                var json = new
                {
                    Success = result.errmsg == "ok",
                    Message = result.errmsg
                };
                return Json(json);
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = ex.Message };
                return Json(json);
            }
        }

        public ActionResult GetMenu()
        {
            var result = CommonAPIs.CommonApi.GetMenu(ACCESSTOKEN);
            if (result == null)
            {
                return Json(new { error = "菜单不存在或验证失败！" }, JsonRequestBehavior.AllowGet);
            }

            try
            {
                GetCash();
                GetUsers();
                GetCardMembers();
            }
            catch (Exception ex)
            {
            }


            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /*
         *  public static dynamic GetSubscribes(string access_token, string nextOpenId)
        {
            var client = new HttpClient();
            var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}", access_token, nextOpenId)).Result;
            if (!result.IsSuccessStatusCode) return null;
            return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);
        }
        public static dynamic Query(string token)
        {
            var client = new HttpClient();
            var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token)).Result;
            if (!result.IsSuccessStatusCode) return null;
            return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);
        }*/
        public void GetUsers()
        {
            try
            {
                var users = UserApi.Get(ACCESSTOKEN, "");
                foreach (string openid in users.data.openid)
                {
                    var user = UserApi.Info(ACCESSTOKEN, openid);
                    USERS[openid] = user;
                }
                foreach (KeyValuePair<string, object> kp in USERS)
                {
                    CustomApi.SendText(ACCESSTOKEN, kp.Key, "感谢关注我，intex", 10000);
                }
            }
            catch (Exception ex)
            {
            }


        }
        public void GetCash()
        {
            var cards = CardMemberApi.GetCash(ACCESSTOKEN, "1","10");
            foreach (var cash in cards.data.openid)
            {
                 
            }
            

            

        }
        public void GetCardMembers()
        {
            try
            {
                CardMemberInfoJson card = new CardMemberInfoJson();
                card.cardId = "1";

                card.name = "张三";
                card.mobile = "13800000000";
                card.gender = "1";
                card.birthday = "2014-01-23";
                card.cash = "123.20";
                card.score = "600";
                card.gradeId = "81";
                card.status = "0";
                card.from = "1";
                card.storeId = "BJ2";
                CardMemberApi.SetCard(ACCESSTOKEN, card, 10000);
                card.cardId = "2";

                card.name = "张2三";
                card.mobile = "13800000000";
                card.gender = "1";
                card.birthday = "2014-01-23";
                card.cash = "123.20";
                card.score = "600";
                card.gradeId = "81";
                card.status = "0";
                card.from = "1";
                card.storeId = "BJ2";
                CardMemberApi.SetCard(ACCESSTOKEN, card, 10000);

                var cards = CardMemberApi.Info(ACCESSTOKEN, "1");
                CARDS[cards.cardId] = cards;
                cards = CardMemberApi.Info(ACCESSTOKEN, "2");
                CARDS[cards.cardId] = cards;

                foreach (KeyValuePair<string, object> kp in CARDS)
                {
                    card = (CardMemberInfoJson)kp.Value;
                    CardMemberApi.SetCard(ACCESSTOKEN, card, 10000);
                    //CustomApi.SendText(ACCESSTOKEN, kp.Key, "感谢关注我，intex", 10000);
                }


            }
            catch (Exception ex)
            {
            }



        }
        public ActionResult DeleteMenu()
        {
            try
            {
                var result = CommonAPIs.CommonApi.DeleteMenu(ACCESSTOKEN);
                var json = new
                               {
                                   Success = result.errmsg == "ok",
                                   Message = result.errmsg
                               };
                return Json(json, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = ex.Message };
                return Json(json, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
